package com.red.wood.module.eval.dao.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.springjdbc.CommonDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.TaskAuditDao;

@Repository
public class TaskAuditDaoImpl implements TaskAuditDao{

	@Autowired
	private CommonDao dao;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public void add(Map<String, Object> paramMap){
		StringBuilder sqlSb = new StringBuilder();
		sqlSb.append("insert into t_eval_taskAudit (");
		dao.insertTableData(paramMap, sqlSb);
	}
	
	public void update(IMap paramMap){
		String singleTaskId = CommonUtils.valueOf(paramMap.get("singleTaskId"));
		if(StringUtils.isNotBlank(singleTaskId)){
			StringBuilder sqlSb = new StringBuilder();
			sqlSb.append("update t_eval_taskAudit set ");
			String queryConf = " where singleTaskId=" + singleTaskId;
			dao.updateTableData(paramMap, sqlSb, queryConf);
		}
	}
	
	public Map<String, Object> getPageData(Map<String, Object> param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		count = listCount(param);
		list = listPageData(param);
		
		if(list != null && !list.isEmpty()){
			for(Map<String, Object> map: list){
				if(map.get("lastSubmitTime") != null){
					Date lastSubmitTime = (Date)map.get("lastSubmitTime");
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("lastSubmitTime", formatter.format(lastSubmitTime));
				} else {
					map.put("lastSubmitTime", "无");
				}
				if(map.get("createTime") != null){
					Date createTime = (Date)map.get("createTime");
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("createTime", formatter.format(createTime));
				} else {
					map.put("createTime", "无");
				}
				if(map.get("updateTime") != null){
					Date updateTime = (Date)map.get("updateTime");
					SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("updateTime", formatter.format(updateTime));
				} else {
					map.put("updateTime", "无");
				}
			}
		}
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}
	
	@Override
	public List<Map<String, Object>> exportData(IMap param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select t1.singleTaskId,");
		sql.append("       t1.companyName,");
		sql.append("       t1.formNum,");
		sql.append("       t1.business,");
		sql.append("       case when t1.statue = '1' then '未审核' when t1.statue = '2' then '已核实(未评测)' when t1.statue = '3' then'退回修改' when t1.statue = '4' then '评测中'when t1.statue = '5' then '测试通过'when t1.statue = '6' then '测试不通过' when t1.statue = '7' then'退回补正' when t1.statue = '8' then'已补正' end as statue,");
		sql.append("       t2.registerProvince,");
		sql.append("       t2.productionUnit,");
		sql.append("       t1.lastSubmitTime,");
		sql.append("       case when t1.business = 'ISP' or t1.business = 'IDC(不含互联网资源协作)' then t2.gameProvince");
		sql.append("         else (select group_concat(distinct(province)) from t_eval_applicationcdn_node node where node.singletaskid = t1.singletaskid) end as houseAddress");
		sql.append("  from t_eval_taskAudit t1");
		sql.append(" inner join t_eval_application_cdn t2");
		sql.append("    on t1.singleTaskId = t2.singleTaskId");
		sql.append(" order by t1.formNum desc");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public List<Map<String, Object>> listPageData(Map<String, Object> param){
		Page page = (Page)param.get("page");
		int offset = (page.getPageNo() -1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		sql.append("select a.id as id, a.singleTaskId as singleTaskId,companyName as companyName,formNum as formNum, formName as formName, business as business, businessType as businessType, statue as statue, timeLength as timeLength, returnNum as returnNum, lastSubmitTime as lastSubmitTime, createTime as createTime, updateTime as updateTime,b.productionUnit,b.registerProvince,CASE WHEN a.business = 'ISP' OR a.business = 'IDC(不含互联网资源协作)' THEN b.gameProvince ELSE(SELECT group_concat(DISTINCT(province)) FROM t_eval_applicationcdn_node node WHERE node.singletaskid = a.singletaskid)END AS houseAddress from t_eval_taskAudit a,t_eval_application_cdn b where a.singleTaskId=b.singletaskid");
		sql.append(getQueryconf(param));
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(Map<String, Object> param) {
		StringBuffer sql = new StringBuffer();
		sql.append("select count(1) as count from t_eval_taskAudit a,t_eval_application_cdn b where a.singleTaskId=b.singletaskid ");
		sql.append(getQueryconf(param));
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer getQueryconf(Map<String, Object> param){
		StringBuffer queryConf = new StringBuffer();
		String companyName = CommonUtils.valueOf(param.get("companyName"));
		if(StringUtils.isNotBlank(companyName)){
			queryConf.append(" and companyName like concat('%', '"+companyName+"', '%')");
		}
		String statue = CommonUtils.valueOf(param.get("statue"));
		if(StringUtils.isNotBlank(statue)){
			queryConf.append(" and statue=" + statue);
		} 
		String businessType = CommonUtils.valueOf(param.get("businessType"));
		if(StringUtils.isNotBlank(businessType)){
			queryConf.append(" and businessType=" + businessType);
		} 
		String formName = CommonUtils.valueOf(param.get("formName"));
		if(StringUtils.isNotBlank(formName)){
			queryConf.append(" and formName=" + formName);
		} 
		String returnNum = CommonUtils.valueOf(param.get("returnNum"));
		if(StringUtils.isNotBlank(returnNum)){
			queryConf.append(" and returnNum=" + returnNum);
		} 
		String timeLength = CommonUtils.valueOf(param.get("timeLength"));
		if(StringUtils.isNotBlank(timeLength)){
			queryConf.append(" and timeLength=" + timeLength);
		} 
		String createTimeAfter = CommonUtils.valueOf(param.get("createTimeAfter"));
		if(StringUtils.isNotBlank(createTimeAfter)){
			queryConf.append(" and createTime <=" + createTimeAfter);
		} 
		String createTimeBefore = CommonUtils.valueOf(param.get("createTimeBefore"));
		if(StringUtils.isNotBlank(createTimeBefore)){
			queryConf.append(" and createTime >=" + createTimeBefore);
		} 
		String business = CommonUtils.valueOf(param.get("business"));
		if(StringUtils.isNotBlank(business)){
			queryConf.append(" and business like '%"+business+"%'");
		} 
		String formNum = CommonUtils.valueOf(param.get("formNum"));
		if(StringUtils.isNotBlank(formNum)){
			queryConf.append(" and formNum like '%"+formNum+"%'");
		} 
		String address = CommonUtils.valueOf(param.get("address"));
		if(StringUtils.isNotBlank(address)){
			queryConf.append(" and registerProvince like '%"+address+"%'");
		} 
		String controctor = CommonUtils.valueOf(param.get("controctor"));
		if(StringUtils.isNotBlank(controctor)){
			queryConf.append(" and productionUnit like '%"+controctor+"%'");
		} 
		queryConf.append(" order by createTime desc");
		return queryConf;
	}

	@Override
	public Map<String, Object> getBySingleTaskId(String singleTaskId) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select id as id, singleTaskId as singleTaskId,companyName as companyName,formNum as formNum, formName as formName, business as business, businessType as businessType, statue as statue, timeLength as timeLength, returnNum as returnNum, lastSubmitTime as lastSubmitTime, createTime as createTime, updateTime as updateTime from t_eval_taskAudit where singleTaskId = " + singleTaskId;
			List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
			if(null != list && list.size() > 0){
				return list.get(0);
			}
		}
		return null;
	}

	@Override
	public int getCountBySingleTaskId(String singleTaskId) {
		if(StringUtils.isNotBlank(singleTaskId)){
			String sql = "select count(1) as count from t_eval_taskAudit where singleTaskId="+singleTaskId;
			Map<String, Object> map = jdbcTemplate.queryForList(sql).get(0);
			String count = map.get("count").toString();
			if(StringUtils.isNotBlank(count)){
				return Integer.valueOf(count);
			}
		}
		return 0;
	}
}
